package com.vint.leetcode;

import com.vint.common.TreeNode;

/*
 * 1. done
 * 2. easy
 * 3. tree, 递归
 * 判断是否有从根到叶子的路径，使得路径之和为给定的值
 */
public class Leetcode112 {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(null==root){
            return false;
        }
        if(null==root.left&&null==root.right){
            return root.val==sum;
        }
        return hasPathSum(root.left,sum-root.val) || hasPathSum(root.right, sum-root.val);
    }
    public static void main(String[] args) {
        Leetcode112 leetcode100= new Leetcode112();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(5);
        root.right = new TreeNode(4);
        root.right.right= new TreeNode(2);
        TreeNode root1 = new TreeNode(1);
        root1.left = new TreeNode(2);
        root1.left.left = new TreeNode(5);
        root1.right = new TreeNode(4);
        root1.right.right= new TreeNode(3);
        System.out.println(leetcode100.hasPathSum(root,8));
    }
  }
